﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Castle.Core.Interceptor;

namespace EAV.Bridge
{
    public class RelationInterceptor : IInterceptor
    {
        string _foreignKeyName;
        Guid _foreignKeyValue;

        public RelationInterceptor(string foreignKeyName, Guid foreignKeyValue)
        {
            _foreignKeyName = foreignKeyName;
            _foreignKeyValue = foreignKeyValue;
        }

        #region IInterceptor Members

        public void Intercept(IInvocation invocation)
        {
            switch (invocation.Method.Name)
            {
                case "Add":

                    BaseItem item = invocation.Arguments.Length == 1 ?
                        invocation.Arguments[0] as BaseItem :
                        null;

                    if (item != null)
                    {
                        item.SetForeignKey(_foreignKeyName, _foreignKeyValue);
                    }

                    break;
            }

            invocation.Proceed();
        }

        #endregion
    }
}
